package com.gzyz.cheetah.config;

import com.gzyz.cheetah.exception.AbstractServiceException;
import com.gzyz.cheetah.util.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 *
 * @author xclhove
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * ExceptionHandler相当于controller的@RequestMapping,如果抛出的的是ServiceException，则调用该方法
     *
     * @param serviceException 异常
     * @return 异常信息
     */
    @ExceptionHandler(AbstractServiceException.class)
    @ResponseBody
    public Result<String> serviceHandle(HttpServletResponse response, AbstractServiceException serviceException) {
        response.setContentType("application/json");
        return Result.error(serviceException.getStatus(), serviceException.getMessage());
    }
}
